package uk.ac.strath.cis.religionsearch.view.components;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import uk.ac.strath.cis.religionsearch.controller.Controller;
import uk.ac.strath.cis.religionsearch.view.IndexWindow;



public class MenuBar {
	
	private JMenuBar menuBar;
	private JMenu fileMenu;
	private JMenu helpMenu;
	private JMenuItem exitMenuItem;
	private JMenuItem indexManger;
	private JMenuItem aboutMenuItem,
					  helpMenuItem;
	private JDialog aboutDialog,
					helpDialog;
	private JPanel aboutContentPane,
					helpPane;
	private JLabel aboutVersionLabel,
	 			   helpLabel;
	
	private JFrame destFrame;
	private Controller c;
	
	
	public JMenuBar getMenuBar(JFrame _destFrame, Controller _c) {
		if (menuBar == null) {
			destFrame = _destFrame;
			c=_c;
			menuBar = new JMenuBar();
			menuBar.add(getFileMenu());
			menuBar.add(getHelpMenu());
		}
		return menuBar;
	}

	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getIndexMangerMenuItem());
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(getAboutMenuItem());
			helpMenu.add(getHelpMenuItem());
		}
		return helpMenu;
	}

	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	private JMenuItem getIndexMangerMenuItem() {
		if (indexManger == null) {
			indexManger = new JMenuItem();
			indexManger.setText("Index Manager");
			indexManger.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					IndexWindow iw = new IndexWindow(c);
					JFrame iwf = iw.getIndexWindow();
					iwf.pack();
					Point loc = destFrame.getLocation();
					loc.translate(20, 20);
					iwf.setLocation(loc);
					iwf.setVisible(true);
				}
			});
		}
		return indexManger;
	}
	
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem();
			aboutMenuItem.setText("About");
			aboutMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JDialog aboutDialog = getAboutDialog();
					aboutDialog.pack();
					Point loc = destFrame.getLocation();
					loc.translate(20, 20);
					aboutDialog.setLocation(loc);
					aboutDialog.setVisible(true);
				}
			});
		}
		return aboutMenuItem;
	}
	
	private JDialog getAboutDialog() {
		if (aboutDialog == null) {
			aboutDialog = new JDialog(destFrame, true);
			aboutDialog.setTitle("About");
			aboutDialog.setSize(new Dimension(354, 206));
			aboutDialog.setResizable(false);
			aboutDialog.setPreferredSize(new Dimension(354, 206));
			aboutDialog.setMinimumSize(new Dimension(354, 206));
			aboutDialog.setContentPane(getAboutContentPane());
		}
		return aboutDialog;
	}
	
	private JPanel getAboutContentPane() {
		if (aboutContentPane == null) {
			aboutContentPane = new JPanel();
			aboutContentPane.setLayout(null);
			aboutContentPane.add(getAboutVersionLabel(), null);
		}
		return aboutContentPane;
	}
	
	private JLabel getAboutVersionLabel() {
		if (aboutVersionLabel == null) {
			aboutVersionLabel = new JLabel();
			aboutVersionLabel.setText("<html><h3>Religion Search Pro</h3><p/>Developed by Group 2, Multimedia Information Access, cis dept, Strathclyde. &copy; 2010<p/><p>MICHAEL LEES,<br/>AIDAN CORDNER,<br/>ANTOINE DESCHARMES,<br/>DAVID DUNCAN,<br/> ALAN FAULKNER<br/><br/></p></html>");
			aboutVersionLabel.setBounds(new Rectangle(15, 16, 320, 140));
			aboutVersionLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return aboutVersionLabel;
	}
	
	private JMenuItem getHelpMenuItem() {
		if (helpMenuItem == null) {
			helpMenuItem = new JMenuItem();
			helpMenuItem.setText("Help");
			helpMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JDialog helpDialog = getHelpDialog();
					helpDialog.pack();
					Point loc = destFrame.getLocation();
					loc.translate(20, 20);
					helpDialog.setLocation(loc);
					helpDialog.setVisible(true);
				}
			});
		}
		return helpMenuItem;
	}
	
	
	private JDialog getHelpDialog() {
		if (helpDialog == null) {
			helpDialog = new JDialog(destFrame, true);
			helpDialog.setTitle("Help");
			helpDialog.setSize(new Dimension(350, 500));
			helpDialog.setResizable(false);
			helpDialog.setPreferredSize(new Dimension(350, 500));
			helpDialog.setMinimumSize(new Dimension(350, 500));
			helpDialog.setContentPane(getHelpPane());
		}
		return helpDialog;
	}
	
	private JPanel getHelpPane() {
		if (helpPane == null) {
			helpPane = new JPanel();
			helpPane.setLayout(null);
			helpPane.add(getHelpLabel(), null);
		}
		return helpPane;
	}
	
	private JLabel getHelpLabel() {
		if (helpLabel == null) {
			helpLabel = new JLabel();
			helpLabel.setText("<html><h3>Searching Advanced Queries</h3><p>It is possible to use boolean operators for a more specific search. For example if you type \"Jesus AND wept\" it would only return verses containing both words. Also supported is NOT; to exclude a word from the search and OR; to include either words in the search (this is the default when two or more words are entered)</p><p>It is also possible to search for a specfic testament, book title or tag. To do this type either;<br/>title:your query here - this is for testament titles such as new testament<br/>bktlong:your query here - this is for book titles e.g. Paul<br/>tags:your query here - this is for user added tags</p></html>");
			helpLabel.setBounds(new Rectangle(10, 10, 300, 490));
			helpLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return helpLabel;
	}
}
